package cn.Y_Qiu.user.service.impl;

import cn.Y_Qiu.basic.exception.BusinessException;
import cn.Y_Qiu.basic.service.impl.BaseServiceImpl;
import cn.Y_Qiu.basic.util.StrUtils;
import cn.Y_Qiu.user.domain.LoginInfo;
import cn.Y_Qiu.user.domain.User;
import cn.Y_Qiu.user.domain.UserDto;
import cn.Y_Qiu.user.mapper.LoginInfoMapper;
import cn.Y_Qiu.user.mapper.UserMapper;
import cn.Y_Qiu.user.service.IUserService;
import cn.Y_Qiu.user.util.MD5Utils;
import cn.Y_Qiu.user.util.UserCodeutil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void phoneRegister(UserDto userDto) {
        //1 基本校验
        //1.1 空检验
        if (userDto.getPassword() == null ||
                userDto.getPhone() == null ||
                userDto.getConfirmPwd() == null ||
                userDto.getVerifyCode() == null ){
            throw new BusinessException("请输入完整参数");
        }
        //1.2 两个密码要一致
        if (!(userDto.getConfirmPwd().equals(userDto.getPassword()))){
            throw new BusinessException("两次输入密码不一致");
        }
        //1.3 判断是否已经被注册
        if (userMapper.queryByPhone(userDto.getPhone()) != null){
            throw new BusinessException("手机号已经被注册");
        }
        //2 验证验证码-吧输入验证码和redis中存的验证码做比对（不要区分大小写）
        String userDtoVerifyCode = userDto.getVerifyCode();
        Object codevalue = redisTemplate.opsForValue().get(UserCodeutil.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        System.out.println(codevalue.toString());
        if (codevalue == null){
            throw new BusinessException("验证码已失效");
        }
        String code = "";
        code = ((String)codevalue).split(":")[0];
        // 验证码校验
        if (!userDto.getVerifyCode().equals(code)){
            throw new BusinessException("验证码输入不正确,请重试");
        }
        // dto转存到user
        User user = UserDto2User(userDto);
        // user的数据转存到logininfo
        LoginInfo info = User2Logininfo(user);
        //3 注册
        //3.2 登录信息表
        loginInfoMapper.add(info);
        user.setLogininfo_id(info.getId());
        //3.1 用户表
        userMapper.add(user);

    }

    private LoginInfo User2Logininfo(User user) {
        LoginInfo info = new LoginInfo();
        BeanUtils.copyProperties(user, info);
        return info;
    }

    private User UserDto2User(UserDto userDto) {
        User user = new User();
        // phone传值
        user.setPhone(userDto.getPhone());
        // phone作为username传值
        user.setUsername(userDto.getPhone());
        // 获取32为随机字符串
        String salt = StrUtils.getComplexRandomString(32);
        // md5加密
        String securitykey = MD5Utils.encrypByMd5(salt + userDto.getPassword());
        user.setSalt(salt);
        user.setPassword(securitykey);
        return user;
    }


}
